package com.fxl.majiang.websocket;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fengxinglei
 * @date 2022/8/3 21:45
 * @description 客户端连接管理
 */
public class ClientManager {

    // 连接池，key为userId
    private static ConcurrentHashMap<String, WebSocketServer> CLIENTS = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, WebSocketServer> getClients() {
        return CLIENTS;
    }

    public static WebSocketServer get(String userId) {
        return CLIENTS.get(userId);
    }

    public static void add(String userId, WebSocketServer client) {
        if (CLIENTS.get(userId) != null) {
            CLIENTS.remove(userId);
        }
        CLIENTS.put(userId, client);
    }

    public static void remove(String userId) {
        CLIENTS.remove(userId);
    }

    public static void clearAll(){
        CLIENTS.clear();
    }
}
